package com.shawn.jetpackstady.paging;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.paging.DataSource;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PagedList;

import com.shawn.jetpackstady.paging.bean.Concert;
import com.shawn.jetpackstady.paging.bean.ConcertFactory;

public class ConcertViewModel extends ViewModel {
    private final LiveData<PagedList<Concert>> convertList;
    private DataSource<Integer, Concert> concertDataSource;

    public ConcertViewModel() {
        ConcertFactory concertFactory = new ConcertFactory();
        concertDataSource = concertFactory.create();
        convertList = new LivePagedListBuilder<>(concertFactory, 20).build();
    }

    /**
     * 通知数据源停止加载，并通知它的回调。
     * 如果invalidate已经被调用，则此方法不执行任何操作。
     */
    public void invalidateDataSource() {
        concertDataSource.invalidate();
    }

    public LiveData<PagedList<Concert>> getConvertList() {
        return convertList;
    }
}
